/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package org.gradleshell.plugin

import org.gradle.api.Plugin
import org.gradle.api.Project
import org.apache.commons.lang.StringUtils
import java.io.*;
/**
 * 
 * @author jxcommerce<g2commerce@gmail.com>
 */
class GradleShellPlugin implements Plugin<Project>{
    static String DEFAULT_DEBUG_PORT = "9999"
    Project buildProject;
    public void apply(Project project) {
        this.buildProject = project
        project.task("sl-run")<<{
            project.tasks.getByName("run"){
                def args=[]
                processSpringLoadedArgs(args)
                processDebugArgs(args)
                println("GradleShellPlugin set jvm args: ")
                println(args)
                jvmArgs(args)
            }
        }
    }
    def processDebugArgs(List args){
        if (isDebug()) {
            String debugPort = getDebugPort()
            String dbgOption = '-Xrunjdwp:transport=dt_socket,server=y,address='+debugPort
            if(isSuspend()){
                dbgOption = dbgOption+",suspend=y"
            }else{
                dbgOption = dbgOption+",suspend=n"
            }
            args<<'-Xdebug'<<dbgOption
        }
    }
    def String getDebugPort(){
        String port = System.getProperty('dbgport', '')
        if(StringUtils.isEmpty(port)){
            port = System.getProperty('DBGPORT', DEFAULT_DEBUG_PORT)
        }
        return port;
    }
    def boolean isSuspend(){
        boolean c1 = System.getProperty('suspend', 'false') == 'true'
        boolean c2 = System.getProperty('SUSPEND', 'false') == 'true'
        return c1||c2
    }
    def boolean isDebug(){
        boolean c1 = System.getProperty('debug', 'false') == 'true'
        boolean c2 = System.getProperty('DEBUG', 'false') == 'true'
        return c1||c2
    }
    def processSpringLoadedArgs(List args){
        String slPath = findSLPath();
        if(!StringUtils.isEmpty(slPath)){
            args<<"-javaagent:"+slPath
            args<<"-noverify"
        }
    }
    def String findSLPath(){
        String path = System.getProperty("SLPath");
        if(StringUtils.isEmpty(path)){
            String home = System.getenv("GRADLE_SHELL_HOME");
            if(!StringUtils.isEmpty(home)){
                String libDir = home+"/lib"
                path = findSpringLoaded(libDir)
            }
        }
        return path;
        
    }
    def String findSpringLoaded(String libDir){
        try{
            File fdir = new File(libDir)
            if(fdir.exists){
                File[] files = fdir.listFiles(new FilenameFilter(){
                    public boolean accept(File f, String name){
                        if(f.isFile() &&name.indexOf("springloaded")!=-1){
                            return true;
                        }
                    }
                });
                if(files!=null &&files.length>1){
                    return files[0].getAbsolutePath()
                }else{
                    return null;
                }
            }else{
                return null;
            }
        }catch(Exception e){
            project.getLogger().error("error: ",e)
            return null
        }
    }
}

